package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Application;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.ApplicationService;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myAddApplicationServlet", value = "/servlet/addapplicationservlet")
public class AddApplicationServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(AddApplicationServlet.class);
	private ApplicationService applicationService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" AddApplicationServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		logger.info(" AddApplicationServlet >doGet end.......");
		PrintWriter out = response.getWriter();
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" AddApplicationServlet >doPost start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		ApplicationService applicationServiceImp = wac.getBean("applicationService", ApplicationService.class);
		this.applicationService=applicationServiceImp;
		String applicationContent = request.getParameter("applicationContent");
		String beginTime = request.getParameter("applicationBegin");
		String endTime = request.getParameter("applicationEnd");
		String approvaluser = request.getParameter("selectapprovaluser");
		logger.info("开始时间begintime为："+beginTime);
		logger.info("开始时间begintime为："+endTime);
		Date currentDate = new Date();
		logger.info("当前时间为："+currentDate);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
		Date beginDate = null;
		Date endDate = null;
		try {
			simpleDateFormat.parse(beginTime+"00:00:00");
			simpleDateFormat.parse(endTime+"00:00:00");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info("开始时间为："+beginDate);
		logger.info("结束时间为："+endDate);
		Application application = new Application(applicationContent,beginDate,endDate,currentDate);
		HttpSession session = request.getSession();
		User userLogin = (User)session.getAttribute("userLogin");
		application.setApprovalUser(userLogin);
		User approvalUser = new User();
		approvalUser.setUserName(approvaluser);
		application.setApprovalUser(approvalUser);
		boolean result = applicationService.insertApplication(application);
		if(result) {
			response.sendRedirect(""+request.getContextPath()+"/applicationHandler/showNoSubmitApplication.do");
		}else {
			response.sendRedirect(""+request.getContextPath()+"/addapplicationerror.jsp");
		}
		logger.info(" AddApplicationServlet >doPost end.......");
	}

}